
/*
 * Copyright 2008 WorldWide Conferencing, LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 */

package us.esme.lib

import us.esme._
import model._

import scala.xml.{NodeSeq, Node, Text}

import net.liftweb._
import util._
import http._
import Helpers._

import java.text.SimpleDateFormat

object IsIE7 extends SessionVar[Boolean]({
    // "Firefox/3"
    // "MSIE 7"
    def findUserAgent(in: List[(String, String)]): Can[String] =
    in.filter(_._1.equalsIgnoreCase("User-Agent")).map(_._2).firstOption

  val r: Can[Boolean] =
  for (session <- S.session;
       agent <- findUserAgent(session.initialHeaders)) yield
  agent.indexOf("MSIE 7") >= 0

  r openOr false
})

trait MsgFormat {
  
  def formatMsg(in: Message, showReply: Boolean, showConv: Boolean): NodeSeq =
  formatMsg(in, showReply, showConv, Empty)

  def formatMsg(in: Message, showReply: Boolean, showConv: Boolean,
                inside: Can[() => NodeSeq]): NodeSeq =
  <div class="msg">
	 	<div class="msg-image">
	 	{
     		val r: Can[NodeSeq] =
     		for (user <- in.author.obj;
          		image <- user.image) yield <a href={"/user/"+urlEncode(user.nickname)}><img src={image} alt=""/></a>

     			r openOr <xml:group>&nbsp;</xml:group>
   		}
	 	</div>

		<div class="msg-body">
         	<p class="msg-text">{in.digestedXHTML}</p>

         	<p class="msg-tags"> 	
          	{
            	in.tags.map(tag => <a href={"/tag/"+urlEncode(tag)}>{tag}</a>)
           	}
		 	</p>

	     	<p class="msg-date">{in.author.obj.map(u => <a href={"/user/"+urlEncode(u.nickname)}>{u.niceName}</a>).openOr("")}:
	          { dateFormat(in.when)} 
		 	</p>


			<p class="msg-ctrls">	
				{
                	<a href={"javascript:setReplyTo("+in.id+","+
                                        in.getText.encJs+
                                        ")"}>reply</a>
				}

   				{
                	in.conversation.can.toList.map(cid => <span>| <a href={"/conversation/"+cid}>conversation</a></span>)
				}
			</p>
		

		</div>

    {
      inside.map(_()).openOr(Text(""))
    }
  </div>




  private def dateFormat(in: Long): String = {
    val delta = ((millis - in) / 1000L).toInt

    delta match {
      case 1 => "1 Second ago"
      case n if n < 60 => ""+n+" Seconds ago"
      case n if n >= 60 && n < 120 => "1 Minute ago"
      case n if n < 3600 => ""+(n / 60)+" Minutes ago"
      case n =>
        (n / 3600) match {
          case 1 => "1 Hour ago"
          case n if n < 24 => ""+n+" Hours ago"
          case _ => val df = new SimpleDateFormat("MM/dd HH:mm zzz")
            dateFormatter.format(new java.util.Date(in))
        }
    }
  }
}
